﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MyDBconnection;
using Cat;
using System.Data.Odbc;

namespace Thesis_2
{
    public partial class frmMain : Form
    {
        private string online;
        ConnectionString cs = new ConnectionString();
        OdbcConnection con = null;
        OdbcCommand cmd = null;
        OdbcDataReader rdr = null;
        DatabaseConnection dbconn = new DatabaseConnection();

        public frmMain()
        {
            InitializeComponent();
            //frmPurchaseOrder frmtest = new frmPurchaseOrder();
            //frmtest.ShowDialog();

            mnuExit.Click += new EventHandler(mnuExit_Click);
            timer1.Tick += new EventHandler(timer1_Tick);
            this.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
            this.Load += new EventHandler(frmMain_Load);
            mnuProductEntry.Click += new EventHandler(mnuProductEntry_Click);
            mnuCategory.Click += new EventHandler(mnuCategory_Click);
            mnuUserReg.Click += new EventHandler(mnuUserReg_Click);
            mnuPrice.Click += new EventHandler(mnuPrice_Click);
            mnuSupplierEntry.Click += new EventHandler(mnuSupplierEntry_Click);
            mnuSupplierList.Click += new EventHandler(mnuSupplierList_Click);
            mnuStock.Click += new EventHandler(mnuStock_Click);
            mnuCustomerEntry.Click += new EventHandler(mnuCustomerEntry_Click);
            mnuListCustomer.Click += new EventHandler(mnuListCustomer_Click);
            mnuLogout.Click += new EventHandler(mnuLogout_Click);
            mnuPOS.Click += new EventHandler(mnuPOS_Click);
            mnuSalesRecord.Click += new EventHandler(mnuSalesRecord_Click);
            mnusalesreport.Click += new EventHandler(mnusalesreport_Click);
            mnuLOP.Click += new EventHandler(mnuLOP_Click);
            mnuLOU.Click += new EventHandler(mnuLOU_Click);
            mnuCheque.Click += new EventHandler(mnuCheque_Click);
            mnuCashD.Click += new EventHandler(mnuCashD_Click);
            mnuPurchaseOrder.Click += new EventHandler(mnuPurchaseOrder_Click);
            mnuLogin.Click += new EventHandler(mnuLogin_Click);
        }

        void mnuLogin_Click(object sender, EventArgs e)
        {
            frmLogin frmL = new frmLogin(this);
            frmL.MdiParent = this;
            frmL.Show();
        }

        void mnuPurchaseOrder_Click(object sender, EventArgs e)
        {
            frmPurchaseOrder frmPO = new frmPurchaseOrder();
            frmPO.MdiParent = this;
            frmPO.Show();
        }
        void mnuCashD_Click(object sender, EventArgs e)
        {
            frmCashDenomination frmCD = new frmCashDenomination();
            frmCD.MdiParent = this;
            frmCD.Show();
        }
        void mnuCheque_Click(object sender, EventArgs e)
        {
            frmChequeEntry frmP = new frmChequeEntry();
            frmP.MdiParent = this;
            frmP.Show();
        }
        void mnuLOU_Click(object sender, EventArgs e)
        {
            frmListofuser frmLU = new frmListofuser();
            frmLU.MdiParent = this;
            frmLU.Show();
        }
        void mnuLOP_Click(object sender, EventArgs e)
        {
            frmProductList frmPL = new frmProductList();
            frmPL.MdiParent = this;
            frmPL.Show();
        }
        void mnusalesreport_Click(object sender, EventArgs e)
        {
            frmSalesReport frmS = new frmSalesReport();
            frmS.MdiParent = this;
            frmS.Show();
        }
        void mnuSalesRecord_Click(object sender, EventArgs e)
        {
            frmSalesR frmR = new frmSalesR();
            frmR.MdiParent = this;
            frmR.Show();   
        }
        void mnuPOS_Click(object sender, EventArgs e)
        {
            frmPOS frmP = new frmPOS();
            frmP.MdiParent = this;
            frmP.Show();
        }
        void mnuLogout_Click(object sender, EventArgs e)
        {
            DialogResult result;
            result = MessageBox.Show("Are you sure you want to Log out?", "Log-out", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (result == System.Windows.Forms.DialogResult.Yes)
            {
                frmMain frmM = new frmMain();
                frmM.Show();
                dbconn.dbconnection();
                dbconn.AddData("Insert into tbluserlog(Username)" + "VALUES ('" + lblUser.Text + "')");
                dbconn.DBClose();
                this.Hide();
                mnuLogin.Enabled = true;
                mnuLogout.Enabled = false;
 
               
            }
            else
            {
                this.Show();

            }
        }
        void mnuPrice_Click(object sender, EventArgs e)
        {
            frmProductPrice frmP = new frmProductPrice();
            frmP.MdiParent = this;
            frmP.Show();
        }
        void mnuListCustomer_Click(object sender, EventArgs e)
        {
            frmCustomerList frmL = new frmCustomerList();
            frmL.MdiParent = this;
            frmL.Show();
        }
        void mnuCustomerEntry_Click(object sender, EventArgs e)
        {
            frmCustomerEntry frmC = new frmCustomerEntry();
            frmC.MdiParent = this;
            frmC.Show();
        }  
        void mnuSubcat_Click(object sender, EventArgs e)
        {
            frmSubCategory frmS = new frmSubCategory();
            frmS.MdiParent = this;
            frmS.Show();

        }
        void mnuStock_Click(object sender, EventArgs e)
        {
            frmStock frmS = new frmStock();
            frmS.MdiParent = this;
            frmS.Show();
            
        }
        void mnuSupplierList_Click(object sender, EventArgs e)
        {
            frmSupplierlist frmSL = new frmSupplierlist();
            frmSL.MdiParent = this;
            frmSL.Show();
            
        }
        void mnuSupplierEntry_Click(object sender, EventArgs e)
        {
            
            frmSupplierEntry frmSE = new frmSupplierEntry();
            frmSE.MdiParent = this;
            frmSE.Show();
            
            
        }
        void mnuUserReg_Click(object sender, EventArgs e)
        {
            frmUserManager frmU = new frmUserManager();
            frmU.MdiParent = this;
            frmU.Show();   
        }
        void mnuCategory_Click(object sender, EventArgs e)
        {
            frmCategory frmC = new frmCategory();
            frmC.MdiParent = this;
            frmC.Show();
        }
        void mnuProductEntry_Click(object sender, EventArgs e)
        {
            frmproduct frmP = new frmproduct();
            frmP.MdiParent = this;
            frmP.Show();
            frmP.lblUser.Text = lblUser.Text;
           
        }
        void frmMain_Load(object sender, EventArgs e)
        {
            frmLogin frm = new frmLogin(this);
            frm.MdiParent = this;
            frm.Show();
            this.Show();

            mnuLogin.Enabled = false;
            mnuLogout.Enabled = false;
            

            userlog();

        }
        void userlog()
        {
            con = new OdbcConnection(cs.DBConn);
            con.Open();
            string us = "Select * from tbluserlog";
            cmd = new OdbcCommand(us);
            cmd.Connection = con;
            rdr = cmd.ExecuteReader();
            while (rdr.Read())
            {
                lblUser.Text = (rdr["Username"].ToString());
            }
        }
        void frmMain_FormClosing(object sender, FormClosingEventArgs e)
        {
            Application.Exit();
        }
        void timer1_Tick(object sender, EventArgs e)
        {
            DateTime dt = DateTime.Now;
            this.ToolStripStatusLabel4.Text = dt.ToString();
        }
        void mnuExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        public void manipulation(string accesslevel)
        {
            switch (accesslevel)
            {
                case "Admin":
                    mnuMasterEntry.Enabled = true;
                    mnuUser.Enabled = true;
                    mnuLogout.Enabled = true;
                    mnuLogin.Enabled = false;
                    mnuSupplier.Enabled = true;
                    mnuCustomer.Enabled = true;
                    mnuProduct.Enabled = true;
                    mnuRecords.Enabled = true;
                    mnuSupplier.Enabled = true;
                    mnuSales.Enabled = true;
                    break;
                case "Cashier":
                    mnuMasterEntry.Enabled = false;
                    mnuUser.Enabled = true;
                    mnuLogout.Enabled = true;
                    mnuLogin.Enabled = false;
                    mnuSupplier.Enabled = false;
                    mnuCustomer.Enabled = true;
                    mnuStock.Enabled = false;
                    mnuRecords.Enabled = false;
                    mnuSupplier.Enabled = false;
                    mnuSales.Enabled = true;
                    mnuProduct.Enabled = false;
                    break;

            }
        }
        
    }
}
